package page.mainPage;

import data.PageInfoModel;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import page.BasePage;
import page.actionPage.FindImgBasePageController;
import page.actionPage.TemplateController;
import utils.Constant;
import utils.DBUtils;
import utils.GlobalVar;

import java.util.List;

public class PageSettingPageController extends BasePage {
    @FXML
    TextField pageName;

    @FXML
    private FindImgBasePageController findImgBasePageController;

    @FXML
    private TemplateController templateController;

    @FXML
    public void initialize() {
        List<PageInfoModel> opendPageList = GlobalVar.getGlobalArrayData(Constant.DB.OPENED_PAGE_LIST, PageInfoModel.class);
        assert opendPageList != null;
        PageInfoModel currentPageLcObject = opendPageList.get(opendPageList.size() - 1);
        db = new DBUtils(Constant.DB.PAGE, currentPageLcObject.getLcObject());
        pageName.setText(currentPageLcObject.getLcObject().getString(Constant.DB.PAGE_NAME));
        findImgBasePageController.initAction(this);
        templateController.initTemplate(submitButton -> {
                    findImgBasePageController.saveImageInDB(db);
                    db.put(Constant.DB.PAGE_NAME, pageName.getText());
                    opendPageList.forEach(e -> {
                        HBox pageLocation = GlobalVar.getMainPage().pageLocation;
                        pageLocation.getChildren().clear();
                        if (GlobalVar.getMainPage().pageLocation.getChildren().size() != 0) {
                            pageLocation.getChildren().add(new Text(">"));
                        }
                        pageLocation.getChildren().add(new Text(lcObject.getString(Constant.DB.PAGE_NAME)));
                    });
                    return this;
                }
        );
    }
}
